home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP14 / MULTI1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  9.1 KB  |  299 lines

  1. /*---------------------------------------
  2.    MULTI1.C -- Multitasking Demo
  3.                (c) Charles Petzold, 1996
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int cyChar ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17.      {
  18.      static char szAppName[] = "Multi1" ;
  19.      HWND        hwnd ;
  20.      MSG         msg ;
  21.      WNDCLASSEX  wndclass ;
  22.  
  23.      wndclass.cbSize        = sizeof (wndclass) ;
  24.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  25.      wndclass.lpfnWndProc   = WndProc ;
  26.      wndclass.cbClsExtra    = 0 ;
  27.      wndclass.cbWndExtra    = 0 ;
  28.      wndclass.hInstance     = hInstance ;
  29.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  30.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  32.      wndclass.lpszMenuName  = NULL ;
  33.      wndclass.lpszClassName = szAppName ;
  34.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  35.  
  36.      RegisterClassEx (&wndclass) ;
  37.  
  38.      hwnd = CreateWindow (szAppName, "Multitasking Demo",
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.           {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.           }
  52.      return msg.wParam ;
  53.      }
  54.  
  55. int CheckBottom (HWND hwnd, int cyClient, int iLine)
  56.      {
  57.      if (iLine * cyChar + cyChar > cyClient)
  58.           {
  59.           InvalidateRect (hwnd, NULL, TRUE) ;
  60.           UpdateWindow (hwnd) ;
  61.           iLine = 0 ;
  62.           }
  63.      return iLine ;
  64.      }
  65.  
  66. // Window 1: Display increasing sequence of numbers
  67. // ------------------------------------------------
  68.  
  69. LRESULT APIENTRY WndProc1 (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  70.      {
  71.      static int   iNum, iLine ;
  72.      static short cyClient ;
  73.      char         szBuffer[16] ;
  74.      HDC          hdc ;
  75.  
  76.      switch (iMsg)
  77.           {
  78.           case WM_SIZE :
  79.                cyClient = HIWORD (lParam) ;
  80.                return 0 ;
  81.  
  82.           case WM_TIMER :
  83.                if (iNum < 0)
  84.                     iNum = 0 ;
  85.  
  86.                iLine = CheckBottom (hwnd, cyClient, iLine) ;
  87.  
  88.                wsprintf (szBuffer, "%d", iNum++) ;
  89.  
  90.                hdc = GetDC (hwnd) ;
  91.                TextOut (hdc, 0, iLine * cyChar, szBuffer, strlen (szBuffer)) ;
  92.                ReleaseDC (hwnd, hdc) ;
  93.  
  94.                iLine++ ;
  95.  
  96.                return 0 ;
  97.           }
  98.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  99.      }
  100.  
  101. // Window 2: Display increasing sequence of prime numbers
  102. // ------------------------------------------------------
  103.  
  104. LRESULT APIENTRY WndProc2 (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  105.      {
  106.      static int   iNum = 1, iLine ;
  107.      static short cyClient ;
  108.      char         szBuffer[16] ;
  109.      int          i, iSqrt ;
  110.      HDC          hdc ;
  111.  
  112.      switch (iMsg)
  113.           {
  114.           case WM_SIZE :
  115.                cyClient = HIWORD (lParam) ;
  116.                return 0 ;
  117.  
  118.           case WM_TIMER :
  119.                do   {
  120.                     if (++iNum < 0)
  121.                          iNum = 0 ;
  122.  
  123.                     iSqrt = (int) sqrt (iNum) ;
  124.  
  125.                     for (i = 2 ; i <= iSqrt ; i++)
  126.                          if (iNum % i == 0)
  127.                               break ;
  128.                     }
  129.                while (i <= iSqrt) ;
  130.  
  131.                iLine = CheckBottom (hwnd, cyClient, iLine) ;
  132.  
  133.                wsprintf (szBuffer, "%d", iNum) ;
  134.  
  135.                hdc = GetDC (hwnd) ;
  136.                TextOut (hdc, 0, iLine * cyChar, szBuffer, strlen (szBuffer)) ;
  137.                ReleaseDC (hwnd, hdc) ;
  138.  
  139.                iLine++ ;
  140.  
  141.                return 0 ;
  142.           }
  143.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  144.      }
  145.  
  146. // Window 3: Display increasing sequence of Fibonacci numbers
  147. // ----------------------------------------------------------
  148.  
  149. LRESULT APIENTRY WndProc3 (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  150.      {
  151.      static int   iNum = 0, iNext = 1, iLine ;
  152.      static short cyClient ;
  153.      char         szBuffer[16] ;
  154.      int          iTemp ;
  155.      HDC          hdc ;
  156.  
  157.      switch (iMsg)
  158.           {
  159.           case WM_SIZE :
  160.                cyClient = HIWORD (lParam) ;
  161.                return 0 ;
  162.  
  163.           case WM_TIMER :
  164.                if (iNum < 0)
  165.                     {
  166.                     iNum  = 0 ;
  167.                     iNext = 1 ;
  168.                     }
  169.  
  170.                iLine = CheckBottom (hwnd, cyClient, iLine) ;
  171.  
  172.                wsprintf (szBuffer, "%d", iNum) ;
  173.  
  174.                hdc = GetDC (hwnd) ;
  175.                TextOut (hdc, 0, iLine * cyChar, szBuffer, strlen (szBuffer)) ;
  176.                ReleaseDC (hwnd, hdc) ;
  177.  
  178.                iTemp  = iNum ;
  179.                iNum   = iNext ;
  180.                iNext += iTemp ;
  181.  
  182.                iLine++ ;
  183.  
  184.                return 0 ;
  185.           }
  186.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  187.      }
  188.  
  189. // Window 4: Display circles of random radii
  190. // -----------------------------------------
  191.  
  192. LRESULT APIENTRY WndProc4 (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  193.      {
  194.      static short cxClient, cyClient ;
  195.      HDC          hdc ;
  196.      int          iDiameter ;
  197.  
  198.      switch (iMsg)
  199.           {
  200.           case WM_SIZE :
  201.                cxClient = LOWORD (lParam) ;
  202.                cyClient = HIWORD (lParam) ;
  203.                return 0 ;
  204.  
  205.           case WM_TIMER :
  206.                InvalidateRect (hwnd, NULL, TRUE) ;
  207.                UpdateWindow (hwnd) ;
  208.  
  209.                iDiameter = rand() % (max (1, min (cxClient, cyClient))) ;
  210.  
  211.                hdc = GetDC (hwnd) ;
  212.  
  213.                Ellipse (hdc, (cxClient - iDiameter) / 2,
  214.                              (cyClient - iDiameter) / 2,
  215.                              (cxClient + iDiameter) / 2,
  216.                              (cyClient + iDiameter) / 2) ;
  217.  
  218.                ReleaseDC (hwnd, hdc) ;
  219.                return 0 ;
  220.           }
  221.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  222.      }
  223.  
  224. // Main window to create child windows
  225. // -----------------------------------
  226.  
  227. LRESULT APIENTRY WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  228.      {
  229.      static char   *szChildClass[] = { "Child1", "Child2",
  230.                                        "Child3", "Child4" } ;
  231.      static HWND    hwndChild[4] ;
  232.      static WNDPROC ChildProc[] = { WndProc1, WndProc2,
  233.                                     WndProc3, WndProc4 } ;
  234.      HINSTANCE      hInstance ;
  235.      int            i, cxClient, cyClient ;
  236.      WNDCLASSEX     wndclass ;
  237.  
  238.      switch (iMsg)
  239.           {
  240.           case WM_CREATE :
  241.                hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
  242.  
  243.                wndclass.cbSize        = sizeof (wndclass) ;
  244.                wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  245.                wndclass.cbClsExtra    = 0 ;
  246.                wndclass.cbWndExtra    = 0 ;
  247.                wndclass.hInstance     = hInstance ;
  248.                wndclass.hIcon         = NULL ;
  249.                wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  250.                wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  251.                wndclass.lpszMenuName  = NULL ;
  252.                wndclass.hIconSm       = NULL ;
  253.  
  254.                for (i = 0 ; i < 4 ; i++)
  255.                     {
  256.                     wndclass.lpfnWndProc   = ChildProc[i] ;
  257.                     wndclass.lpszClassName = szChildClass[i] ;
  258.  
  259.                     RegisterClassEx (&wndclass) ;
  260.  
  261.                     hwndChild[i] = CreateWindow (szChildClass[i], NULL,
  262.                          WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,
  263.                          0, 0, 0, 0, hwnd, (HMENU) i, hInstance, NULL) ;
  264.                     }
  265.  
  266.                cyChar = HIWORD (GetDialogBaseUnits ()) ;
  267.                SetTimer (hwnd, 1, 10, NULL) ;
  268.                return 0 ;
  269.  
  270.           case WM_SIZE :
  271.                cxClient = LOWORD (lParam) ;
  272.                cyClient = HIWORD (lParam) ;
  273.  
  274.                for (i = 0 ; i < 4 ; i++)
  275.                     MoveWindow (hwndChild[i], (i % 2) * cxClient / 2,
  276.                                               (i > 1) * cyClient / 2,
  277.                                 cxClient / 2, cyClient / 2, TRUE) ;
  278.                return 0 ;
  279.  
  280.           case WM_TIMER :
  281.                for (i = 0 ; i < 4 ; i++)
  282.                     SendMessage (hwndChild[i], WM_TIMER, wParam, lParam) ;
  283.  
  284.                return 0 ;
  285.  
  286.           case WM_CHAR :
  287.                if (wParam == '\x1B')
  288.                     DestroyWindow (hwnd) ;
  289.  
  290.                return 0 ;
  291.  
  292.           case WM_DESTROY :
  293.                KillTimer (hwnd, 1) ;
  294.                PostQuitMessage (0) ;
  295.                return 0 ;
  296.           }
  297.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  298.      }
  299.